﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using CLFramework.Extension;

namespace CLFramework.Infrastructure.PlugIns
{
    public class PluginContainer
    {
        private bool _enabled;
        private PlugInBase _iplugIn;
        private Type _plugInType;
        private string _name;
        private XmlNode _node;
        private bool isLoaded;

        internal PluginContainer(Type type, XmlNode node)
        {
            this._enabled = true;
            this._node = node;
            this._plugInType = type;
            this._enabled = node.GetBoolAttribute("enabled", true);
            this._name = node.GetStringAttribute("name", "");
        }

        internal PlugInBase Instance
        {
            get
            {
                if (this.Enabled && (this._iplugIn == null))
                {
                    if (this._plugInType != null)
                    {
                        this._iplugIn = Activator.CreateInstance(this._plugInType, _node) as PlugInBase;
                    }
                    this._enabled = this._iplugIn != null;
                    this.isLoaded = this._enabled;
                }
                return this._iplugIn;
            }
        }

        public bool Enabled
        {
            get
            {
                return this._enabled;
            }
        }

        public bool IsLoaded
        {
            get {
                return isLoaded;
            }
        }

        public Type PluginType
        {
            get
            {
                return this._plugInType;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
        }
    }
}
